package com.liutong;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

public class WordCount {
    public static void main(String[] args) {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSource<String> source = env.fromElements("hello flink", "flink spark storm hbase kafka hbase");
        AggregateOperator<Tuple2<String, Integer>> count = source.flatMap(new Tokenizer()).groupBy(0).sum(1);
    //count.writeAsText("hdfs://node01:9000/wordcount");
        try {
            count.print();

            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class Tokenizer implements FlatMapFunction<String, Tuple2<String, Integer>> {

        @Override
        public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
            for(String word:s.split(" ")){
                collector.collect(new Tuple2<String, Integer>(word,1));
            }
        }
    }
}
